<?php
namespace Framework\Library\Validator;

use Phalcon\Validation;
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;
use Phalcon\Validation\Message;

class IdsValidator extends Validator implements ValidatorInterface
{
    public function validate(Validation $validation, $attribute)
    {
        $ids = $validation->getValue($attribute);

        if (!is_array($ids)) {
            $validation->appendMessage(new Message('Should be an array', $attribute, 'Array'));

            return false;
        }
        if (count($ids) < (int) $this->getOption('min')) {
            $validation->appendMessage(new Message('Array size is too small', $attribute, 'Array'));

            return false;
        }
        if (count($ids) > (int) $this->getOption('max')) {
            $validation->appendMessage(new Message('Array size is too large', $attribute, 'Array'));

            return false;
        }
        foreach ($ids as $id) {
            if (intval($id) . '' !== $id) {
                $validation->appendMessage(new Message('Id format error', $attribute, 'Id'));

                return false;
            }
        }

        return true;
    }
}
